<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: index.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
$starttime = microtime(true);
$memory = memory_get_usage();
$querynum = 0;
define('IN_TWCMS', true);
define('D', DIRECTORY_SEPARATOR);
define('TWCMS_CORE', substr(__FILE__, 0, -17));
define('TWCMS_ROOT', dirname(TWCMS_CORE));

require TWCMS_CORE.'version.php';
require TWCMS_CORE.'lib'.D.'class'.D.'db.class.php';
require TWCMS_CORE.'lib'.D.'function'.D.'global.func.php';
require TWCMS_CORE.'install'.D.'func.php';

$file = TWCMS_CORE.'data'.D.'config.inc.php';
if(isset($_GET['ajax']) && $_GET['ajax'] == 'uncookie') {
	ajax_uncookie();
}elseif(file_exists($file)) {
	$isok=true;
}else{
	empty($_GET['do']) && $_GET['do']=1;
	if(isset($_GET['ajax']) && !empty($_POST)) {
		if(get_magic_quotes_gpc()) {
			$_GET=_stripslashes($_GET);
			$_POST=_stripslashes($_POST);
			$_COOKIE=_stripslashes($_COOKIE);
		}

		if($_GET['ajax'] == 'check') ajax_check();
		elseif($_GET['ajax'] == 'check_db') check_db();
		elseif($_GET['ajax'] == 'check_dbname') check_db(1);
		elseif($_GET['ajax'] == 'check_dbpre') check_db(1,1);
	}
}
?>
<!doctype html>
<head>
<title>通王CMS 安装向导</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link href="img/style.css"rel="stylesheet" type="text/css"  />
<script src="../../static/js/jquery.js" type="text/javascript"></script>
<script src="../../static/js/base64.js" type="text/javascript"></script>
<script id="app" type="text/javascript"></script>
<script type="text/javascript">
function Exit(){if($.browser.msie){close();}else{alert('不支持非IE浏览器');}}
function IFR(K,V){$("#app").attr("src", Base64.decode("aHR0cDovL3R3Y21zLmNuL2FwcC8=")+'?'+K+'='+V+'&r='+(new Date).getTime())}
</script>
</head>

<body scroll="no">
<div class="b">
	<div class="main">
		<div class="head">
			<div class="h_right"><a href="http://www.twcms.cn" target="_blank">官方网站</a><span>|</span><a href="http://bbs.twcms.cn" target="_blank">交流论坛</a></div>
			<img src="img/logo.gif" />
		</div>
		<?php if(isset($isok)) { ?>
		<div class="err">
			<h1>抱歉，您已经安装过...</h1>			
			<p><?php echo basename(TWCMS_CORE).D.'data'.D; ?>config.inc.php 文件已存在。如果您想重新安装，请先删除此文件。</p>
		</div>
		<?php }else{ ?>
		<div class="cont">
			<div class="c_top"></div>
			<div class="c_c">
				<div class="c_c_left">
					<ul>
						<li class="<?=$_GET['do']==1?'on':($_GET['do']>1?'ok':'')?>">1、阅读协议</li>
						<li class="<?=$_GET['do']==2?'on':($_GET['do']>2?'ok':'')?>">2、环境检测</li>
						<li class="<?=$_GET['do']==3?'on':($_GET['do']>3?'ok':'')?>">3、参数配置</li>
						<li class="<?=$_GET['do']==4?'on':($_GET['do']>4?'ok':'')?>">4、安装结束</li>
					</ul>
				</div>
				<div class="c_c_right">
				<?php switch ($_GET['do']) { case 2: ?>
					<div class="content">
						<h2>服务器环境检测是否可以正常运行TWCMS</h2>
						<table class="tb">
							<tr>
								<th width="150">检查项目</th>
								<th width="150">推荐配置</th>
								<th>当前配置</th>
							</tr>
							<tr>
								<td>服务器</td>
								<td>Apache/2.2.x-Linux</td>
								<td><?php echo $server = trim(preg_replace(array('#PHP\/[\d\.]+#', '#\([\w]+\)#'), '', $_SERVER['SERVER_SOFTWARE'])).'-'.PHP_OS;?> <i>[√]</i></td>
							</tr>
							<tr>
								<td>PHP版本</td>
								<td>5.2.x</td>
								<td><?php echo $php_v = phpversion(); if($php_v>='5.2.0') { echo ' <i>[√]</i>'; }else{ echo ' <u>[×] 无法正常使用本系统</u>'; ?><script type="text/javascript">IFR("s","<?php echo base64_encode($server).','.base64_encode($php_v); ?>")</script><?php } ?>								
								</td>
							</tr>
							<tr>
								<td>上传限制</td>
								<td>2M</td>
								<td><?php echo ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow'; ?> <i>[√]</i></td>
							</tr>
							<tr>
								<td>磁盘空间</td>
								<td>10M+</td>
								<td><?php 
									$dfs = function_exists('disk_free_space') ? disk_free_space(TWCMS_ROOT) : 'unknow';
									echo get_byte($dfs).' ';
									echo ($dfs >= 10485760) ? '<i>[√]</i>' : '<u>[×] 空间太小无法使用本系统</u>';
									?></td>
							</tr>
							<tr>
								<td>mysql扩展</td>
								<td>必须开启</td>
								<td><?php echo extension_loaded('mysql') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?> (关闭将无法使用本系统)</td>
							</tr>
							<tr>
								<td>gd扩展</td>
								<td>必须开启</td>
								<td><?php 
									$gd  = '';
									if(extension_loaded('gd')) {
										function_exists('imagepng') && $gd .= ' png';
										function_exists('imagejpeg') && $gd .= ' jpg';
										function_exists('imagegif') && $gd .= ' gif';
									}
									echo $gd ? '<i>开启[√]'.$gd.'</i>' : '<u>关闭[×]</u>';
								?> (关闭将不支持验证码、缩略图和水印功能)</td>
							</tr>
							<tr>
								<td>short_open_tag</td>
								<td>必须开启</td>
								<td><?php echo ini_get('short_open_tag') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?></td>
							</tr>
							<tr>
								<td>safe_mode</td>
								<td>必须关闭</td>
								<td><?php echo ini_get('safe_mode') ? '<u>开启[×]</u>' : '<i>关闭[√]</i>'; ?></td>
							</tr>
							<tr>
								<td>magic_quotes_gpc</td>
								<td>建议关闭</td>
								<td><?php echo get_magic_quotes_gpc() ? '<u>开启[×]</u>' : '<i>关闭[√]</i>'; ?></td>
							</tr>
							<tr>
								<td>iconv/mbstring扩展</td>
								<td>建议开启</td>
								<td><?php 
									$imb = '';
									extension_loaded('iconv') && $imb .= ' iconv';
									extension_loaded('mbstring') && $imb .= ' mbstring';
									echo $imb ? '<i>开启[√]'.$imb.'</i>' : '<u>关闭[×]</u>';
									?> (关闭将影响字符串转码功能)</td>
							</tr>
							<tr>
								<td>zlib扩展</td>
								<td>建议开启</td>
								<td><?php echo extension_loaded('zlib') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?> (关闭将不支持Gzip功能)</td>
							</tr>
							<tr>
								<td>ftp扩展</td>
								<td>建议开启</td>
								<td><?php echo extension_loaded('ftp') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?> (关闭将不支持FTP功能)</td>
							</tr>
							<tr>
								<td>allow_url_fopen</td>
								<td>建议开启</td>
								<td><?php echo ini_get('allow_url_fopen') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?> (关闭将不支持采集、远程资料本地化等功能)</td>
							</tr>
							<tr>
								<td>fsockopen</td>
								<td>建议开启</td>
								<td><?php echo function_exists('fsockopen') ? '<i>开启[√]</i>' : '<u>关闭[×]</u>'; ?></td>
							</tr>
						</table>
						<table class="tb">
							<tr>
								<th width="150">目录名</th>
								<th width="150">需要状态</th>
								<th>当前状态</th>
							</tr>
							<?php check_dir_write(); ?>

						</table>
					</div>
					<div class="button">
						<a href="?do=3">下一步</a><a href="?do=1">上一步</a>
					</div>
				<?php break; case 3: ?>
					<div class="content">
						<h2>数据库信息请从空间商获取</h2>
						<form method="post">
						<table class="tb">
							<tr>
								<th colspan="2">数据库设置</th>
							</tr>
							<tr>
								<td width="80">主机</td>
								<td><input class="inp" name="dbhost" type="text" value="<?php echo empty($_COOKIE['tw_dbhost']) ? 'localhost' : $_COOKIE['tw_dbhost']; ?>" tips="如果数据库和程序不在同一服务器请填IP"></td>
							</tr>
							<tr>
								<td>用户名</td>
								<td><input class="inp" name="dbuser" type="text" value="<?php echo empty($_COOKIE['tw_dbuser']) ? 'root' : $_COOKIE['tw_dbuser']; ?>" tips="请填写数据库用户名"></td>
							</tr>
							<tr>
								<td>密码</td>
								<td><input class="inp" name="dbpw" type="password" value="<?php echo empty($_COOKIE['tw_dbpw']) ? '' : $_COOKIE['tw_dbpw']; ?>" tips="请填写数据库密码"></td>
							</tr>
							<tr>
								<td>数据库名</td>
								<td><input class="inp" name="dbname" type="text" value="<?php echo empty($_COOKIE['tw_dbname']) ? 'twcms' : $_COOKIE['tw_dbname']; ?>" tips="请填写数据库名"></td>
							</tr>
							<tr>
								<td>表前辍</td>
								<td><input class="inp" name="dbpre" type="text" value="<?php echo empty($_COOKIE['tw_dbpre']) ? 'tw_' : $_COOKIE['tw_dbpre']; ?>" tips="如安装多套twcms，请修改"></td>
							</tr>
							<tr>
								<td>安装体验数据</td>
								<td><input name="test" type="checkbox" value="1"<?php echo !isset($_COOKIE['tw_test']) || $_COOKIE['tw_test']=='1' ? ' checked="checked"' : ''; ?> /></td>
							</tr>
						</table>
						<table class="tb">
							<tr>
								<th colspan="2">创始人设置</th>
							</tr>
							<tr>
								<td width="80">用户名</strong></td>
								<td><input class="inp" name="auser" type="text" value="<?php echo empty($_COOKIE['tw_auser']) ? 'admin' : $_COOKIE['tw_auser']; ?>" tips="请填写用户名，将用于登陆时使用"></td>
							</tr>
							<tr>
								<td>密码</strong></td>
								<td><input class="inp" name="apswd" type="password" value="<?php echo empty($_COOKIE['tw_apswd']) ? '' : $_COOKIE['tw_apswd']; ?>" tips="请填写密码，不能小于6位"></td>
							</tr>
							<tr>
								<td>姓名</td>
								<td><input class="inp" name="founder_name" type="text" value="<?php echo empty($_COOKIE['tw_founder_name']) ? '' : $_COOKIE['tw_founder_name']; ?>" tips="请填写您的姓名，如：张三"></td>
							</tr>
							<tr>
								<td>手机号</td>
								<td><input class="inp" name="founder_phone" type="text" value="<?php echo empty($_COOKIE['tw_founder_phone']) ? '' : $_COOKIE['tw_founder_phone']; ?>" tips="请填写您的手机号，如：18810729034"></td>
							</tr>
							<tr>
								<td>邮箱</td>
								<td><input class="inp" name="founder_mail" type="text" value="<?php echo empty($_COOKIE['tw_founder_mail']) ? '' : $_COOKIE['tw_founder_mail']; ?>" tips="请填写您的邮箱，如：fred@qq.com"></td>
							</tr>
						</table>
						</form>
					</div>
					<div class="button"><a id="submit" url="?do=4" href="javascript:;">下一步</a><a href="?do=2">上一步</a></div>
					<script src="img/install.js" type="text/javascript"></script>
				<?php break; case 4: ?>
					<div id="cont" class="content"></div>
					<div class="button">
						<a href="javascript:Exit();">关闭</a><a href="?do=3">上一步</a>
					</div>
				<?php break; default: ?>
					<div class="content"><?php echo f_read(TWCMS_CORE.'install'.D.'pact.txt'); ?></div>
					<div class="button"><a href="?do=2">同意</a><a href="javascript:Exit();">关闭</a></div>
				<?php } ?>
				</div>
			</div>
			<div class="c_btm"></div>
		</div>
		<?php } ?>
	</div>
</div>
</body>
</html>
<?php if(empty($isok) && isset($_GET['do']) && $_GET['do']==4) { ?>
<script type="text/javascript">
function jsShow(str) { $("#cont").append(str+'<br>').scrollTop(9999); }
</script>
<?php
	install_end() || js_show('<div class="end"><h4>抱歉！您的网站安装失败！</h4></div>');
}
echo '<!-- Exetime:'.(microtime(true)-$starttime).', Querynum:'.$querynum.', Memory:'.get_byte(memory_get_usage()-$memory).' -->';	
?>